var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), e = require("../../@babel/runtime/helpers/toConsumableArray"), a = require("../../@babel/runtime/helpers/asyncToGenerator"), r = require("../../utils/switchTab"), n = getApp(), i = require("../../utils/api.js"), s = require("../../http/productApi.js");

Page({
    data: {
        systemInfo: n.data.systemInfo,
        menuIndex: 0,
        menuList: [],
        bannerList: [],
        productList: [],
        hasMore: !0,
        loadingMore: !1
    },
    onLoad: function(t) {
        console.log(t), this.waterFallView = this.selectComponent("#waterFallView"), t.cateId && (n.data.directCateId = t.cateId);
    },
    onShow: function() {
        (0, r.switchToTab)(this, 1), (0 == this.data.menuList.length || n.data.directCateId) && this.loadCateList();
    },
    menuItemOnTap: function(t) {
        if (!this.data.loadingMore) {
            var e = t.currentTarget.dataset.index;
            e != this.data.menuIndex && (this.setData({
                menuIndex: e
            }), this.loadProductList(!0));
        }
    },
    scrollOnBottom: function() {
        this.loadProductList(!1);
    },
    loadCateList: function() {
        var r = this;
        return a(t.default.mark(function a() {
            var i, d, o;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, n.showLoading(), t.next = 4, s.directCateList();

                  case 4:
                    i = t.sent, d = [ {
                        categoryId: void 0,
                        categoryName: "全部"
                    } ].concat(e(i.data)), r.setData({
                        menuList: d
                    }), n.data.directCateId && (o = d.findIndex(function(t) {
                        return t.categoryId == n.data.directCateId;
                    }), r.setData({
                        menuIndex: o
                    }), n.data.directCateId = void 0), r.loadProductList(!0), t.next = 14;
                    break;

                  case 11:
                    t.prev = 11, t.t0 = t.catch(0), console.log(t.t0);

                  case 14:
                    return t.prev = 14, wx.hideLoading(), t.finish(14);

                  case 17:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 0, 11, 14, 17 ] ]);
        }))();
    },
    toCar: function() {
        n.global.loginThenRedirectTo("/pages/car/car");
    },
    loadProductList: function(r) {
        var n = this;
        return a(t.default.mark(function a() {
            var d, o, c, u, l, h, p;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (r && (n.setData({
                        productList: [],
                        hasMore: !0
                    }), n.waterFallView.initData(null, null)), n.data.hasMore && !n.data.loadingMore) {
                        t.next = 3;
                        break;
                    }
                    return t.abrupt("return");

                  case 3:
                    if (d = 20, o = n.data.productList.length / d + 1, c = n.data.menuList[n.data.menuIndex].categoryId, 
                    n.setData({
                        loadingMore: !0
                    }), t.prev = 7, u = new Date().getTime(), n.data.bannerList && 0 != n.data.bannerList.length) {
                        t.next = 14;
                        break;
                    }
                    return t.next = 12, s.directBannerList();

                  case 12:
                    l = t.sent, n.setData({
                        bannerList: l.data
                    });

                  case 14:
                    return t.next = 16, s.directProductList(o, d, {
                        categoryId: c
                    });

                  case 16:
                    return h = t.sent, t.next = 19, i.delay(u, 500);

                  case 19:
                    p = [].concat(e(n.data.productList), e(h.data.list)), n.setData({
                        inited: !0,
                        productList: p,
                        hasMore: h.data.totalPage > h.data.currPage
                    }), 1 == h.data.currPage ? n.waterFallView.initData(0 == n.data.menuIndex ? n.data.bannerList : null, h.data.list) : n.waterFallView.appendProducts(h.data.list), 
                    t.next = 27;
                    break;

                  case 24:
                    t.prev = 24, t.t0 = t.catch(7), console.log(t.t0);

                  case 27:
                    return t.prev = 27, n.setData({
                        loadingMore: !1
                    }), t.finish(27);

                  case 30:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 7, 24, 27, 30 ] ]);
        }))();
    }
});